﻿using System;

namespace WF_AppFuel.Base.Helpers
{
    /// <summary>
    /// Generic Range class representing a range of values.
    /// This class is patterned after Martin Fowler's Range pattern.
    /// </summary>
    /// <author>Arjay C. Nacion</author>
    /// <typeparam name="T">The type for this Range</typeparam>
    public class Range<T> : IEquatable<Range<T>>
        where T : IComparable<T>, IEquatable<T>
    {
        private readonly T start;
        private readonly T end;

        public Range(T start, T end)
        {
            this.start = start;
            this.end = end;
        }

        public T Start
        {
            get { return this.start; }
        }

        public T End
        {
            get { return this.end; }
        }

        public bool Contains(T value)
        {
            return this.start.CompareTo(value) <= 0 &&
                value.CompareTo(end) <= 0;
        }

        public bool Contains(Range<T> value)
        {
            return this.Contains(value.Start) && this.Contains(value.End);
        }

        public override int GetHashCode()
        {
            return this.start.GetHashCode() ^ this.end.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is Range<T>)
                return Equals((Range<T>)obj);
            else
                return false;
        }

        public bool Equals(Range<T> other)
        {
            return this.start.Equals(other.Start) &&
                this.end.Equals(other.End);
        }

    }
}
